package com.smyhvae.viewpagerdemo.viewpager.viewpager;

import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import static android.content.ContentValues.TAG;

/**
 * @author Lin·俊浩
 * @since 2017/9/29
 */

public class MyPageTransformer implements ViewPager.PageTransformer{

    private static final float MIN_SCALE = 0.3f;

    @Override
    public void transformPage(View view, float position) {

        float pageWidth = view.getWidth();

        if (position < -1){//前一个View position = -1
            view.setAlpha(0);
        }else if (position <= 0){
            view.setTranslationX(pageWidth * -position);//与原本的切换效果抵消，使得页面在原位置不动
            float leftScaleFactor = MIN_SCALE
                    + (1 - MIN_SCALE) * (1 - Math.abs(position));
            view.setAlpha(1 - Math.abs(position));
            view.setScaleX(leftScaleFactor);
            view.setScaleY(leftScaleFactor);
        }else if (position <= 1){
            view.setTranslationX(pageWidth * -position);
            Log.d(TAG, "transformPage: "+pageWidth);
            Log.d(TAG, "transformPage: "+position);
            float rightScaleFactor = MIN_SCALE
                    + (1 - MIN_SCALE) * (1 - Math.abs(position));
            view.setAlpha(1 - Math.abs(position));
            view.setScaleX(rightScaleFactor);
            view.setScaleY(rightScaleFactor);
        }else {//后一个View position = -1
            view.setAlpha(0);
        }
    }
}
